package com.cskaoyan.dbutils;

import com.cskaoyan.opensource.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 创建日期: 2022/11/18 16:22
 *
 * @author ciggar
 */
public class QueryRunnerDemo1 {

    public static void main(String[] args) throws SQLException {

        Connection connection = DruidUtils.getConnection();

        // 1. 构造一个QueryRunner

        // 无参构造
        // 当使用无参构造方法构造出来的QueryRunner，在去执行SQL语句的时候，需要传入Connection对象
         QueryRunner queryRunner1 = new QueryRunner();
        // 有参构造
        // 当使用有参构造的方法构造出来的QueryRunner，在执行SQL语句的时候，不需要传入Connection对象
         QueryRunner queryRunner2 = new QueryRunner(DruidUtils.getDataSource());


        // 2. 通过QueryRunner执行SQL语句
//        int affectedRows = queryRunner1.update(connection,"insert into account values (2001,'张飞',10000)");
//        System.out.println(affectedRows);

        int affectedRows = queryRunner2.update("insert into account values (?,?,?)", 3001, "叶文洁", 3000);
        System.out.println(affectedRows);


//        DataSource dataSource = queryRunner.getDataSource();
//        Connection connection = dataSource.getConnection();
//        queryRunner.update(connection,"insert into account values (2001,'张飞',10000)");


    }
}
